package com.controllers;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;

import com.daos.CommentDao;
import com.model.Activity;
import com.model.Comment;
import com.model.Employee;
import com.model.Task;
import com.utils.DateUtils;

@Named
@ManagedBean
public class CommentController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4339096739351526449L;

	private TaskController taskController;

	private CommentDao commentDao;

	public CommentController() {
	}

	@Inject
	public CommentController(TaskController taskController,
			CommentDao commentDao) {
		this.commentDao = commentDao;
		this.taskController = taskController;
	}

	public int save(Comment newComment, Task selectedTask, Employee employee, int selectedProgress) {
		
		newComment.setDate(DateUtils.getCurrentDate());
		newComment.setBeforeProgress(selectedTask.getProgress());
		newComment.setTask(selectedTask);
		newComment.setEmployee(employee);
		commentDao.save(newComment);
		selectedTask.setProgress(selectedProgress);
		int result = taskController.calculateActivityProgress(selectedTask, true);
		Activity activity = selectedTask.getActivity();
		activity.setProgress(result);
		taskController.save(selectedTask);
		
		return result;

		
	}

	public CommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public List<Comment> getCommentsForTask(long id) {
		return commentDao.getCommentsForTask(id);
	}

	public List<Activity> getAllComments() {
		return commentDao.getAllComments();
	}

}
